package com.wangsh.forward.web.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wangsh.forward.system.pojo.ASysProperty;
import com.wangsh.forward.system.pojo.ASysPropertyEnum;
import com.wangsh.forward.system.service.ISystemOperService;
import com.wangsh.forward.system.service.ISystemService;
import com.wangsh.forward.system.service.impl.SystemOperServiceImpl;
import com.wangsh.forward.system.service.impl.SystemServiceImpl;
import com.wangsh.webforward.common.pojo.ApiResponse;
import com.wangsh.webforward.common.servlet.BaseServlet;
import com.wangsh.webforward.common.util.ConstatFinalUtil;

/**
 * 不需要登陆,就能访问的Servlet
 * @author TeaBig
 */
public class MainHeadServlet extends BaseServlet
{
	private static final long serialVersionUID = 1L;
	
	private ISystemService systemService;
	private ISystemOperService systemOperService;

	/**
	 * 初始化对象
	 * @throws ServletException
	 */
	public void initObject()
	{
		if(this.systemService == null)
		{
			this.systemService = new SystemServiceImpl() ;
		}
		
		if(this.systemOperService == null)
		{
			this.systemOperService = new SystemOperServiceImpl() ; 
		}
	}
	/**
	 * Servlet的核心方法,不管是Post还是get都要执行此方法
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		super.service(request, response);
		
		/* 接收一个method参数 */
		String method = request.getParameter("method");
		if ("main".equalsIgnoreCase(method))
		{
			this.main(request, response);
		}else if("init".equalsIgnoreCase(method))
		{
			/* 初始化 */
			this.init(request,response);
		}else if("initSubmit".equalsIgnoreCase(method))
		{
			/* 初始化 */
			this.initSubmit(request,response);
		}else if("reload".equalsIgnoreCase(method))
		{
			/* 重新加载数据 */
			this.reload(request,response);
		}else if("sysPropertyList".equalsIgnoreCase(method))
		{
			/* 系统配置列表 */
			this.sysPropertyList(request,response);
		}else if("sysPropertyUpdate".equalsIgnoreCase(method))
		{
			/* 系统配置查看 */
			this.sysPropertyUpdate(request,response);
		}else if("sysPropertyUpdateSubmit".equalsIgnoreCase(method))
		{
			/* 系统配置更新提交 */
			this.sysPropertyUpdateSubmit(request,response);
		}
	}

	/**
	 * 重新加载数据
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void reload(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		ConstatFinalUtil.SYS_LOGGER.info("==reload==");
		ApiResponse<ASysProperty> apiResponse = this.systemOperService.reloadPropertyService();
		request.setAttribute("response", apiResponse.toJSON());
		this.infoPage(request, response);
		return ; 
	}

	/**
	 * 系统配置更新提交
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void sysPropertyUpdateSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("==sysPropertyUpdate==");
		String path = "/WEB-INF/jsp/head/sysPropertyUpdate.jsp" ; 
		
		String id = request.getParameter("id");
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据id查询 */
		condMap.clear();
		condMap.put("id", id);
		ApiResponse<ASysProperty> apiResponse = this.systemService.findOnePropertyService(condMap);
		ASysProperty sysProperty = apiResponse.getDataOneJava() ;
		
		/* 接收参数,更新信息 */
		String value = request.getParameter("value");
		String status = request.getParameter("status");
		String content = request.getParameter("content");
		
		sysProperty.setValue(value);
		sysProperty.setStatus(Byte.valueOf(status));
		sysProperty.setContent(content);
		
		ApiResponse<Object> apiResponseDb = this.systemService.updateOnePropertyService(sysProperty);
		/* 存储信息 */
		request.setAttribute("response", apiResponseDb.toJSON());
		/* 服务器端跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 查看系统配置详情
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void sysPropertyUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		ConstatFinalUtil.SYS_LOGGER.info("==sysPropertyUpdate==");
		String path = "/WEB-INF/jsp/head/sysPropertyInfo.jsp" ; 
		String id = request.getParameter("id");
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据id查询 */
		condMap.clear();
		condMap.put("id", id);
		ApiResponse<ASysProperty> apiResponse = this.systemService.findOnePropertyService(condMap);
		/* 清理信息 */
		this.clearInfo(request, response,apiResponse, "true");
		request.setAttribute("response", apiResponse.toJSON());
		
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			path = "/WEB-INF/jsp/head/sysPropertyUpdate.jsp" ; 
		}
		/* 服务器端跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 系统配置列表
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void sysPropertyList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("==sysPropertyList==");
		/* 列表页面 */
		String path = "/WEB-INF/jsp/head/sysPropertyList.jsp" ; 
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 查询所有的系统配置记录 */
		condMap.clear();
		ApiResponse<ASysProperty> apiResponse = this.systemService.findCondListPropertyService(null, condMap);
		/* 清理信息 */
		this.clearInfo(request, response,apiResponse, "true");
		request.setAttribute("response", apiResponse.toJSON());
		/* 服务器端跳转 */
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 初始化提交
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void initSubmit(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("==initSubmit==");
		
		/* 接收911_path的路径 */
		String path911 = request.getParameter("path_911");
		if(path911 == null)
		{
			path911 = "" ;
		}
		/* 对路径进行替换 */
		path911 = path911.replaceAll("\\\\", "/");
		ASysProperty sysProperty = new ASysProperty();
		sysProperty.setName("path.abs911");
		sysProperty.setValue(path911);
		sysProperty.setContent("911_S5的绝对路径");
		sysProperty.setStatus(ASysPropertyEnum.STATUS_ENABLE.getStatus());
		sysProperty.setCreateTime(new Date());
		sysProperty.setUpdateTime(new Date());
		sysProperty.setPubTime(new Date());
		ApiResponse<Object> apiResponse = this.systemService.saveOnePropertyService(sysProperty);
		ConstatFinalUtil.SYS_LOGGER.info("存储结果:{}",apiResponse.toJSON().toJSONString());
		
		/* 存储tomcat所在的路径
		 * D:\Java\apache-tomcat\webapps\web-forward-web
		 *  */
		String pathTomcat = request.getRealPath("/");
		File pathTomcatFile = new File(pathTomcat);
		/* 要的是tomcat所在的路径 */
		pathTomcatFile = pathTomcatFile.getParentFile().getParentFile() ; 
		pathTomcat = pathTomcatFile.getAbsolutePath().replaceAll("\\\\", "/");
		ASysProperty sysPropertyTomcat = new ASysProperty();
		sysPropertyTomcat.setName("path.tomcat");
		sysPropertyTomcat.setValue(pathTomcat);
		sysPropertyTomcat.setContent("tomcat的绝对路径");
		sysPropertyTomcat.setStatus(ASysPropertyEnum.STATUS_ENABLE.getStatus());
		sysPropertyTomcat.setCreateTime(new Date());
		sysPropertyTomcat.setUpdateTime(new Date());
		sysPropertyTomcat.setPubTime(new Date());
		ApiResponse<Object> apiResponseDb =  this.systemService.saveOnePropertyService(sysPropertyTomcat);
		ConstatFinalUtil.SYS_LOGGER.info("存储结果:{}",apiResponseDb.toJSON().toJSONString());
		
		/* 提交成功,直接跳转到列表页面 */
		response.sendRedirect(request.getContextPath() + "/main.htm?method=sysPropertyList");
		return ; 
	}

	/**
	 * 初始化操作
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("==init==");
		String url = "/WEB-INF/jsp/head/init.jsp" ;
		/* 跳转到初始化页面 */
		/* 说明第一次使用,需要做一些配置 */
		request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * 首页
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("==main==");
		String url = "/WEB-INF/jsp/head/main.jsp" ;
		request.getRequestDispatcher(url).forward(request, response);
	}
}
